//
//  XS_PingtaiZhengshuDetailVC.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/14.
//

import UIKit
import RxSwift
import RxCocoa

class XS_userCertificationM: XS_BaseModel {
    var id = 0 // id
    var updateTime = "" //
    var updateUserId = "" //
    var certificateType = "" //证书类型
    var getTime = "" // 证书获得时间
    var level = "" // 证书级别
    var certificateImgSrc = "" // 证书图片地址
    var certificationName = "" // 证书名称
    var userId = 0 //
    var createTime = "" // 创建时间
    var createUserId = 0 //
    var certificationDescription = "" // 证书描述
}
class XS_pingtaiQueryByIdM: XS_BaseModel {
    var recommendCurriculumIpage = XS_recommendCurriculumIpageM()
    var certificate = XS_certificateM()
    var userCertification = XS_userCertificationM()
    
}

class XS_PingtaiZhengshuDetailVC: XS_BaseVC {
    var id: Int!
    lazy var changeModel = XS_recommendCurriculumIpageM()
    
    override func upConfig() {
        isStatusBarBlack = false
        shouldNavBarHidden = true
    }
    override func upView() {
        title = "证书详情"
        
        let imgV = UIImageView(image: UIImage(named: "首页-证书背景"))
        view.addSubview(imgV)
        imgV.snp.makeConstraints({
            $0.left.top.right.equalToSuperview()
            $0.height.equalTo(.scale*190)
        })
        loadCustomBar()
        
        let scroll = UIScrollView()
        scroll.bounces = false
        scroll.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        scroll.clipsToBounds = true
        scroll.contentInsetAdjustmentBehavior = .never
        view.addSubview(scroll)
        scroll.snp.makeConstraints({
            $0.top.equalTo(customBar.snp.bottom)
            $0.left.right.bottom.equalToSuperview()
        })
        let top = UIImageView(image: UIImage(named: "首页-证书背景"))
        scroll.addSubview(top)
        top.snp.makeConstraints({
            $0.left.width.equalToSuperview()
            $0.height.equalTo(.scale*190)
            $0.top.equalToSuperview().offset(-CGFloat.barHeight - 44)
        })
        
        let cImgV = UIImageView()
        cImgV.layer.masksToBounds = true
        cImgV.layer.cornerRadius = .scale*4
        cImgV.layer.shadowColor = UIColor.red.cgColor;//阴影颜色
        cImgV.layer.shadowOffset = CGSize(width: 5,height: 5);//偏移距离
        cImgV.layer.shadowOpacity = 0.8;//不透明度
        cImgV.layer.shadowRadius = 5;//半径
        
        scroll.addSubview(cImgV)
        cImgV.snp.makeConstraints {
            $0.top.equalTo(.scale*22)
            $0.centerX.equalToSuperview()
            $0.width.equalTo(.scale*285)
            $0.height.equalTo(.scale*200)
        }
        let tLab = UILabel.xsLabel(size: .scale*18, fontName: .semibold, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), alignment: .center)
        scroll.addSubview(tLab)
        tLab.snp.makeConstraints({
            $0.centerX.equalToSuperview()
            $0.top.equalTo(cImgV.snp.bottom).offset(.scale*22)
        })
        
        let backView = UIView()
        backView.layer.corner(radius: .scale*4, bgColor: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), isShadow: true, sColor: #colorLiteral(red: 0.1843137255, green: 0.2039215686, blue: 0.2039215686, alpha: 1), offset: CGSize(width: 0, height: .scale*2), effect: .scale*12, opacity: 0.06)
        scroll.addSubview(backView)
        backView.snp.makeConstraints({
            $0.width.equalTo(.scale*343)
            $0.centerX.equalToSuperview()
            $0.top.equalTo(tLab.snp.bottom).offset(.scale*17)
        })
        
//        let dLab = UILabel.xsLabel(size: .scale*12, fontName: .regular, color: #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1), lines: 0)
//        scroll.addSubview(dLab)
//        dLab.snp.makeConstraints({
//            $0.left.equalTo(.scale*16)
//            $0.right.equalTo(-.scale*16)
//            $0.width.equalTo(view.frame.size.width - 2 * .scale*16)
//            $0.top.equalTo(backView.snp.bottom).offset(.scale*16)
//        })
        
        let line = UIView()
        line.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        scroll.addSubview(line)
        line.snp.makeConstraints({
            $0.height.equalTo(.scale*8)
            $0.top.equalTo(backView.snp.bottom).offset(.scale*16)
            $0.left.width.equalToSuperview()
            $0.bottom.lessThanOrEqualToSuperview().offset(-CGFloat.bottomHeight)
        })
        
//        let collections = XD_RecommendCollView()
//        scroll.addSubview(collections)
//        collections.snp.makeConstraints({
//            $0.top.equalTo(line.snp.bottom)
//            $0.height.equalTo(.scale*160)
//            $0.bottom.lessThanOrEqualToSuperview().offset(-CGFloat.bottomHeight)
//            $0.left.width.equalToSuperview()
//        })

        XS_NetWork.requestJson("/userCertification/queryById", parameters: ["id":id!, "pageSize":0])
            .mapModel(type: XS_pingtaiQueryByIdM.self)
            .subscribe(onNext: { [weak self] model, resp in
                guard let `self` = self, resp.success, let model = model else { return }
                cImgV.sd_setImage(with: URL(string: model.userCertification.certificateImgSrc))
                tLab.text = model.userCertification.certificationName
                
                let tempTitlesArr = ["级别","获得时间"]
                var titlesArr: [String] = []
                var detialsArr: [String] = []
                
                for (index, text) in [
                    model.userCertification.level,
                    model.userCertification.getTime
                ].enumerated() {
                    if text.count > 0 {
                        detialsArr.append(text)
                        titlesArr.append(tempTitlesArr[index])
                    }
                }
                
                self.getItemViews(superView: backView, titlesArr: titlesArr, detailsArr: detialsArr)
                
//                dLab.text = model.certificate.description
//                collections.certificateId = model.certificate.id
//                collections.reLoadData()
            }).disposed(by: disposeBag)
    }
    
    private func getItemViews(superView: UIView, titlesArr: [String], detailsArr: [String]) {
        
        var lastView: UIView?
        
        for (index, text) in detailsArr.enumerated() {
            let textView = UIView()
            superView.addSubview(textView)
            textView.snp.makeConstraints({
                if let lastV = lastView {
                    $0.top.equalTo(lastV.snp.bottom)
                } else {
                    $0.top.equalToSuperview()
                }
                if index == detailsArr.count - 1 {
                    $0.bottom.equalToSuperview()
                }
                $0.left.equalTo(.scale * 13)
                $0.right.equalTo(-.scale * 13)
            })
            
            let titlesLab = UILabel.xsLabel(size: .scale*14, fontName: .regular, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), text: titlesArr[index])
            textView.addSubview(titlesLab)
            titlesLab.snp.makeConstraints({
                $0.top.equalTo(.scale * 14)
                $0.left.equalTo(.scale * 27)
                $0.width.equalTo(.scale * 76)
                $0.height.equalTo(.scale * 20)
            })
            
            let detailsLab = UILabel.xsLabel(size: .scale*14, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1), text: text)
            detailsLab.numberOfLines = 0
            textView.addSubview(detailsLab)
            detailsLab.snp.makeConstraints({
                $0.top.equalTo(titlesLab)
                $0.left.equalTo(titlesLab.snp.right)
                $0.right.equalTo(-.scale * 27)
            })
            
            let lineV = UIView()
            lineV.isHidden = false
            lineV.backgroundColor =  UIColor(red: 0.90, green: 0.91, blue: 0.95, alpha: 1.0)
            textView.addSubview(lineV)
            lineV.snp.makeConstraints { (make) in
                make.top.equalTo(detailsLab.snp.bottom).offset(.scale * 13)
                make.left.right.equalToSuperview()
                make.height.equalTo(.scale * 0.5)
                make.bottom.equalToSuperview()
            }
            
            if index == detailsArr.count - 1 {
                lineV.isHidden = true
            }
            
            lastView = textView
        }
    }
}
